//
//  NSURL+DoNotBackup.m
//  TravelGuideMdd
//
//  Created by 陈曦 on 13-5-17.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "NSURL+DoNotBackup.h"
#import "sys/xattr.h"

@implementation NSURL(DoNotBackup)

- (instancetype)setDoNotBackup
{
    if ([[UIDevice currentDevice].systemVersion floatValue]>5.09) {//for iOS5.1 and later
        NSError *error = nil;
        BOOL success = [self setResourceValue: [NSNumber numberWithBool: YES]
                                      forKey: NSURLIsExcludedFromBackupKey error: &error];
        if(!success){
            NSLog(@"Error excluding %@ from backup %@", [self lastPathComponent], error);
        }
        
    }else{//for iOS 5.0.1
        
        const char* filePath = [[self path] fileSystemRepresentation];
        
        const char* attrName = "com.apple.MobileBackup";
        u_int8_t attrValue = 1;
        
        setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    }
    return self;
}

@end
